package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2023-01-05
 * Time: 17:15
 */

/**
 * 统一异常处理
 */
@ControllerAdvice
@ResponseBody
public class ErrorAdvice {

    @ExceptionHandler(Exception.class)
    public HashMap<String, Object> exceptionAdvice(Exception e){
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", "-1");
        result.put("msg", e.getMessage());
        return result;
    }


    @ExceptionHandler(ArithmeticException.class)
    public HashMap<String, Object> ArithmeticAdvice(ArithmeticException e){
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", "-2");
        result.put("msg", e.getMessage());
        return result;
    }
}
